package com.atguigu;

import com.atguigu.common.DiffCounter;
import com.atguigu.common.Utils;
import com.atguigu.mapreduce.practice.PracticeDriver;
import com.atguigu.mapreduce.solution.SolutionDriver;
import com.atguigu.mapreduce.source.GeneralSource;
import com.atguigu.mapreduce.source.impl.WordSource;
import org.apache.hadoop.util.ToolRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Runner {
    private static final Logger LOG = LoggerFactory.getLogger(Runner.class);
    // 控制是否提交结果到服务器
    public static final Boolean SUBMIT = true;

    public static void main(String[] args) {
        LOG.error("清理上次的输入文件和运行结果...");
        Utils.delete("input");
        Utils.delete("output");

        LOG.error("生成输入文件...");
        GeneralSource source = new WordSource();
        source.getAsFile("input/a.txt", 30L * 1024 * 1024);
        source.getAsFile("input/b.txt", 30L * 1024 * 1024);
        source.getAsFile("input/c.txt", 30L * 1024 * 1024);

        LOG.error("运行中...");
        try {
            ToolRunner.run(new PracticeDriver(), args);
        } catch (Exception e) {
            LOG.error("似乎计算出现了问题，请检查代码...");
            e.printStackTrace();
            System.exit(1);
        }

        if (SUBMIT) {
            LOG.error("运行答案代码...");
            try {
                ToolRunner.run(new SolutionDriver(), args);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

            LOG.error("比较结果...");
            DiffCounter diff = Utils.fileDiff("output/solution", "output/practice");

            Utils.sendResult(diff.getDiff() == 0);
        }
    }
}
